package org.christopher.mybatisplusdemo;

import lombok.extern.log4j.Log4j;
import org.christopher.mybatisplusdemo.common.JsonData;
import org.christopher.mybatisplusdemo.common.UserCondition;
import org.christopher.mybatisplusdemo.entity.User;
import org.christopher.mybatisplusdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Log4j
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/query_user")
    public JsonData queryUser(@RequestBody UserCondition condition){
//        log.info("日志输出："+condition.getName());
        System.out.println("条件位"+condition.getName());
        List<User> users = userService.selectUserAllByCondition(condition);
        return JsonData.buildSuccess(users);
    }

    @GetMapping("/query_user/{id}")
    public JsonData queryUserById(@PathVariable Integer id){
        User user = userService.selectUserById(id);
        return JsonData.buildSuccess(user);
    }

    @GetMapping("/query_user_ids")
    public JsonData queryUserByIds(){
        List<Integer> ids = new ArrayList<>();
        List<User> users = userService.selectUserByIds(ids);
        return JsonData.buildSuccess(users);
    }

    @PostMapping("/do_login")
    public JsonData doLogin(User user){

        User loginUser = userService.loginUser(user);

        return JsonData.buildSuccess(loginUser);
    }

    @PostMapping("/query_count")
    public JsonData queryCount(@RequestBody User user){
//        log.info(user.toString());
        System.out.println("传入的"+user);
        int i = userService.selectUserCount(user);
        return JsonData.buildSuccess(i);
    }

    @PostMapping("reg")
    public JsonData regUser(@RequestBody User user){
        System.out.println("注册"+user);
        int i = userService.addUser(user);
        return JsonData.buildSuccess(i);
    }

    @GetMapping("/update1")
    public JsonData updateUser1(@RequestBody User user){
        int i = userService.updataUser(user);
        return JsonData.buildSuccess(i);
    }

    @GetMapping("/update2")
    public JsonData updateUser2(@RequestBody User user){
        int i = userService.updateUser2(user);
        return JsonData.buildSuccess(i);
    }
}
